//
//  BaseAlertView.swift
//  funcode
//
//  Created by Wan Jiading on 2017/9/15.
//  Copyright © 2017年 WanJiading. All rights reserved.
//
/**
 *  1.子类必须在 convenience init 开始调用beforeSetupSubViews，末尾调用afterSetupSubviews
 *  2.
 */

import UIKit

class BaseAlertView: UIButton {
    
    /// 承载所有内容的view
    var contentView = UIView()
    
    /// 点击蒙版时，是否消失，默认 true；必须在 afterSetupSubviews 之前设置。
    var dismissWhenClickMaskView = true
    
    func beforeSetupSubViews() {
        contentView.backgroundColor = UIColor.white
        contentView.layer.cornerRadius = 9
        self.addSubview(contentView)
    }
    
    func afterSetupSubviews() {
        if dismissWhenClickMaskView {
            self.addTarget(self, action: #selector(tapSelf), for: .touchUpInside)
        }
    }
    
    /// 设置当前View的frame
    func setupSelfFrame(_ frame: CGRect) {
        self.frame = frame
    }
    
    /// 设置contentSize,全屏、横向、纵向 居中
    func setupContentSize(_ size: CGSize) {
        let screenSize = UIScreen.main.bounds.size
        contentView.center = CGPoint(x: screenSize.width * 0.5, y: screenSize.height * 0.5)
        contentView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
            make.width.equalTo(size.width)
            make.height.equalTo(size.height)
        }
    }
    
    /// 设置contentFrame,全屏、横向、纵向 居中
    func setupContentFrame(_ frame: CGRect) {
        // 主题内容背景view
        contentView.snp.makeConstraints { (make) in
            make.left.equalTo(frame.origin.x)
            make.top.equalTo(frame.origin.y)
            make.width.equalTo(frame.size.width)
            make.height.equalTo(frame.size.height)
        }
    }
    
    func tapSelf() {
        dismiss()
    }
    
    func show() {
        let animation = CAKeyframeAnimation(keyPath: "transform")
        animation.duration = 0.25
        
        let values = [
            NSValue.init(caTransform3D: CATransform3DMakeScale(0.8, 0.8, 1.0)),
            NSValue.init(caTransform3D: CATransform3DMakeScale(1.01, 1.01, 1.0)),
            NSValue.init(caTransform3D: CATransform3DMakeScale(0.99, 0.99, 1.0)),
            NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
            ]
        animation.values = values
        contentView.layer.add(animation, forKey: nil)
        
        self.backgroundColor = UIColor.rgb("000000", alpha: 0.5)
        
        if let window = UIApplication.shared.keyWindow {
            window.addSubview(self)
            
            self.snp.makeConstraints { (make) in
                make.left.equalTo(window.snp.left)
                make.right.equalTo(window.snp.right)
                make.top.equalTo(window.snp.top)
                make.bottom.equalTo(window.snp.bottom)
            }
        }
    }
    
    func dismiss() {
        let animation = CAKeyframeAnimation(keyPath: "transform")
        animation.duration = 0.25
        let values = [
            NSValue.init(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)),
            NSValue.init(caTransform3D: CATransform3DMakeScale(0.8, 0.8, 1.0)),
            NSValue.init(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)),
            ]
        animation.values = values
        contentView.layer.add(animation, forKey: nil)
        
        UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: { [weak self] in
            self?.alpha = 0
        }, completion:{ [weak self] (finished) in
            self?.removeFromSuperview()
        })
    }
    
    deinit {
        DebugLogFunc()
    }
}
